package com.android.entities;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

public class AgendaEntry {

	private String id;
	private String name;
	private String category;
	private String address;
	private String startTime;
	private String endTime;
	private Date date;
	private String description;
	
	public AgendaEntry() { }
	
	public AgendaEntry(JSONObject data) {
		try {
			if (data.has("id") && data.getString("id") != null)
				id = data.getString("id");
			if (data.has("name") && data.getString("name") != null)
				name = data.getString("eventName");
			if (data.has("category") && data.getString("category") != null)
				category = data.getString("category");
			if (data.has("startTime") && data.getString("startTime") != null)
				startTime = data.getString("startTime");
			if (data.has("endTime") && data.getString("endTime") != null)
				endTime = data.getString("endTime");
			if (data.has("date") && data.getString("date") != null)
					setDate(data.getString("date"));
			if (data.has("address") && data.getString("address") != null)
				setDate(data.getString("address"));
			if (data.has("description") && data.getString("description") != null)
				description = data.getString("description");

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}
	
	public Date getDate() {
		return date;
	}
	
	public void setDate(String date) {
		setDate(date, "dd/MM/yyyy");
	}
	
	public void setDate(String date, String pattern) {
		if (date == null || date.length() == 0)
			return;
					
		DateFormat fmt = new SimpleDateFormat(pattern);
		try {
			this.date = new Date(fmt.parse(date).getTime());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getStartTime() {
		return startTime;
	}
	
	public void setStartTime(String time) {
		this.startTime = time;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String eventName) {
		this.name = eventName;
	}
	
	public String getCategory() {
		return category;
	}
	
	public void setCategory(String eventCategory) {
		this.category = eventCategory;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
		
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public JSONObject toJSON() {
		JSONObject result = new JSONObject();
		
		try {
			result.put("id", id);
			result.put("eventName", name);
			result.put("eventCategory", category);
			result.put("startTime", startTime);
			result.put("endTime", endTime);
			result.put("date", date);
			
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return result;
	}
}
